package com.example.demo.service.ipml;

import com.example.demo.Dto.DishDto;
import com.example.demo.Dto.SetmealDto;
import com.example.demo.common.pageResult;
import com.example.demo.entiy.Dish;
import com.example.demo.entiy.DishFlavor;
import com.example.demo.entiy.Setmeal;
import com.example.demo.entiy.SetmealDish;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.mapper.DishMapper;
import com.example.demo.mapper.SetmealDishMapper;
import com.example.demo.mapper.SetmealMapper;
import com.example.demo.service.CategoryServiceImp;
import com.example.demo.service.SetmeaiServiceImp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SetmeaiService implements SetmeaiServiceImp {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;


    @Override
    public pageResult<SetmealDto> getPage(int page, int pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        Page<Setmeal> page1 = setmealMapper.getPage(page,pageSize,name);
        long total = page1.getTotal();
        List<Setmeal> result = page1.getResult();
        List<SetmealDto> list = result.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            String name1 = categoryMapper.getcateName(item.getCategoryId());
            setmealDto.setCategoryName(name1);
            return setmealDto;
        }).toList();
        return pageResult.result(total,list);
    }

    @Override
    public boolean PutInfo(SetmealDto setmealDto) {
        int sum = setmealMapper.insertSelective(setmealDto);
        if (sum!=0){
            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            for (SetmealDish dish : setmealDishes) {
                dish.setCreateTime(setmealDto.getCreateTime());
                dish.setUpdateTime(setmealDto.getUpdateTime());
                dish.setCreateUser(setmealDto.getUpdateUser());
                dish.setUpdateUser(setmealDto.getUpdateUser());
                dish.setSetmealId(setmealDto.getId().toString());
                setmealDishMapper.insertSelective(dish);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean changeStatus(int status, List<Long> ids) {
        return setmealMapper.changeStatus(status,ids) != 0;
    }

    @Override
    public boolean deletedis(List<Long> id) {
        List<Long> ids = setmealMapper.selectById(id);
        if (ids.isEmpty()){
            return false;
        }
        setmealMapper.deleteByid(id);
        return setmealDishMapper.deleteByids(ids) != 0;
    }

    @Override
    public SetmealDto getInfo(Long id) {
        Setmeal setmeal = setmealMapper.selectByPrimaryKey(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        List<SetmealDish> setmealDish = setmealDishMapper.selectById(setmeal.getId());
        setmealDto.setSetmealDishes(setmealDish);
        return setmealDto;
    }

    @Override
    public int UpdataInfo(SetmealDto setmealDto) {
            try{
                setmealMapper.updateByPrimaryKeySelective(setmealDto);
                List<SetmealDish> dishFlavors = setmealDto.getSetmealDishes();
                for (SetmealDish dishFlavor : dishFlavors) {
                    dishFlavor.setUpdateUser(setmealDto.getUpdateUser());
                    dishFlavor.setUpdateTime(setmealDto.getUpdateTime());
                    setmealDishMapper.updateByPrimaryKeySelective(dishFlavor);
                }
                return 1;
            }catch (Exception ignored){
                return 0;
            }
    }
}
